<!-- 2017-12-7 14:56:03 | 修改 木遥（微信:  http://marsgis.cn/weixin.html ） -->
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>路线漫游及控制 卫星示例 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,bootstrap-checkbox,jquery.range,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
    <style>
      .mp_table {
        width: 100%;
        border: #cde1de 1px solid;
        font-size: 12px;
        border-collapse: collapse;
        border-spacing: 0;
      }

      .mp_table tr td {
        padding: 5px 5px;
        text-align: left;
        border: #cde1de 1px solid;
      }

      .mp_table tr td:first-child {
        border-left: none;
      }

      .nametd {
        width: 40%;
      }

      .mp_input {
        height: 28px;
        line-height: 22px;
        padding: 3px 5px;
        box-sizing: border-box;
        font-size: 12px;
        border: 1px solid #e6e6e6;
        background-color: #fff;
        border-radius: 3px;
        display: block;
        width: 100%;
        outline: 0;
        box-sizing: border-box;
      }

      .mp_input:hover {
        border-color: #c9c9c9 !important;
      }

      .mp_input:focus {
        border-color: #3374e0 !important;
      }

      /*select*/
      .mp_select {
        height: 28px;
        line-height: 22px;
        padding: 0 5px;
        box-sizing: border-box;
        font-size: 12px;
        border: 1px solid #e6e6e6;
        background-color: #fff;
        border-radius: 3px;
        outline: 0;
        box-sizing: border-box;
        position: relative;
      }

      .mp_select:after {
        content: '';
        width: 0;
        height: 0;
        overflow: hidden;
        cursor: pointer;
        transition: all 0.2s;
        position: absolute;
        border-width: 6px;
        display: inline-block;
        top: 10px;
        right: 5px;
        border-style: solid dashed dashed;
        border-color: #c2c2c2 transparent transparent;
      }

      .mp_selected:after {
        top: 4px;
        border-style: dashed dashed solid;
        border-color: transparent transparent #c2c2c2;
      }

      .mp_select .mp_select_text {
        padding: 0 20px 0 0;
        height: 28px;
        line-height: 26px;
        overflow: hidden;
      }

      .mp_select_ul {
        background: #fff;
        border: #d6d6d6 1px solid;
        border-radius: 3px;
        padding: 5px 0;
        box-shadow: 1px 2px 1px rgba(0, 0, 0, 0.15);
        position: absolute;
        left: 0;
        z-index: 1000;
        color: #333;
        text-align: center;
        width: 100%;
        display: none;
      }

      .mp_select_ul li {
        margin: 0 5px;
        border-radius: 3px;
        text-align: left;
        height: 28px;
        line-height: 28px;
        text-indent: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .mp_select_ul li:hover {
        background: #ecf1f8;
      }

      .mp_select_ul .selecton,
      .mp_select_ul .selecton:hover {
        background: #3374e0 !important;
        color: #fff !important;
      }

      /*dark*/
      .dark .mp_select {
        background-color: rgba(63, 72, 84, 0.6);
      }

      .dark .mp_select_ul {
        color: #ffffff;
        background-color: rgba(63, 72, 84, 0.8);
      }

      .dark .mp_select_ul li:hover {
        background-color: rgba(63, 72, 84, 1);
      }

      .dark .mp_select_ul .selecton,
      .mp_select_ul .selecton:hover {
        background: rgba(63, 72, 84, 1) !important;
        color: #fff !important;
      }

      .mp_head2 {
        width: 100%;
        height: 45px;
        line-height: 45px;
        text-align: center;
      }

      .dark .progress {
        margin: 0;
        background-color: #0b2931;
      }
    </style>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <!-- 控制 面板 -->
    <div class="infoview">
      <table class="mp_table">
        <tr>
          <td class="nametd">模型角度</td>
          <td>
            <div class="radio radio-info radio-inline">
              <input type="radio" id="jiaodu_1" value="1" name="jiaodu" />
              <label for="jiaodu_1">自动</label>
            </div>
            <div class="radio radio-info radio-inline">
              <input type="radio" id="jiaodu_2" value="2" name="jiaodu" checked />
              <label for="jiaodu_2">手动</label>
            </div>
          </td>
        </tr>
        <tr class="jiaodu_2_angele">
          <td class="nametd">heading值</td>
          <td>
            根据路线自动计算
            <!-- <input id="model_heading" type="range" min="0" max="360" step="0.01" value="10" title="方向"> -->
          </td>
        </tr>
        <tr class="jiaodu_2_angele">
          <td class="nametd">pitch值（前后）</td>
          <td>
            <input id="model_pitch" type="range" min="-90" max="90" step="0.01" value="20" title="倾角pitch" />
          </td>
        </tr>
        <tr class="jiaodu_2_angele">
          <td class="nametd">roll值（左右）</td>
          <td>
            <input id="model_roll" type="range" min="-90" max="90" step="0.01" value="10" title="滚动角roll" />
          </td>
        </tr>
      </table>

      <input type="button" class="btn btn-primary" id="btnClear" value="清除地面投影" />
    </div>

    <!-- 信息 面板 -->
    <div class="infoview rightbottom" style="min-width: 200px">
      <table class="mp_table">
        <tr>
          <td class="nametd">总长度</td>
          <td id="td_alllength"></td>
        </tr>
        <tr>
          <td class="nametd">已漫游长度</td>
          <td id="td_length"></td>
        </tr>
        <tr>
          <td class="nametd">总时长</td>
          <td id="td_alltimes"></td>
        </tr>
        <tr>
          <td class="nametd">已漫游时间</td>
          <td id="td_times"></td>
        </tr>

        <tr>
          <td class="nametd">经度</td>
          <td id="td_jd"></td>
        </tr>
        <tr>
          <td class="nametd">经度</td>
          <td id="td_wd"></td>
        </tr>
        <tr>
          <td class="nametd">漫游高程</td>
          <td id="td_gd"></td>
        </tr>
        <tr>
          <td class="nametd">地面高程</td>
          <td id="td_dmhb"></td>
        </tr>
        <tr id="tr_ldgd">
          <td class="nametd">离地距离</td>
          <td id="td_ldgd"></td>
        </tr>

        <tr>
          <td colspan="2" style="width: 100%; text-align: center">
            <div class="progress">
              <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
            </div>
          </td>
        </tr>
      </table>
    </div>

    <div id="tipView" class="info-tips">
      (1) 不支持对地形的求交，目前仅对椭球体做投射。 <br />
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      'use script' //开发环境建议开启严格模式

      var map
      var roamLine

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: {
              y: 31.344715,
              x: 115.783073,
              z: 10055.81,
              heading: 158.4,
              pitch: -54.6,
              roll: 0.1,
            },
            globe: {
              depthTestAgainstTerrain: true,
            },
          },
        })

        //创建三维地球场景
        map = new mars3d.Map('mars3dContainer', mapOptions)

        map.hasTerrain = false

        //创建矢量数据图层
        var graphicLayer = new mars3d.layer.GraphicLayer()
        map.addLayer(graphicLayer)

        //该数据可以从 基础项目 飞行漫游功能界面操作后保存JSON
        var flydata = {
          speed: 200,
          positions: [
            [115.833866, 31.311451, 4000],
            [115.785116, 31.293944, 4000],
            [115.748115, 31.266263, 4000],
            [115.711031, 31.216472, 4000],
          ],
          model: {
            url: 'http://data.marsgis.cn/gltf/mars/zhanji.glb',
            scale: 0.01,
            minimumPixelSize: 100,
            show: true,
          },
          path: {
            color: '#ffff00',
            width: 3,
            show: true,
          },
          shadow: {
            type: 'wall',
            show: true,
          },
          camera: {
            type: 'zy',
            followedX: 50,
            followedZ: 10,
          },
          clockLoop: false,
          interpolation: false,
          showGroundHeight: true,
          clampToGround: false,
        }

        roamLine = new mars3d.graphic.RoamLine(flydata)
        graphicLayer.addGraphic(roamLine)

        //启动漫游
        roamLine.start()

        testShading()

        initUI()

        if (map.viewer.timeline) {
          map.viewer.timeline.zoomTo(roamLine.startTime, roamLine.stopTime)
        }

        //显示基本信息，名称、总长、总时间
        $('#td_alltimes').html(haoutil.str.formatTime(roamLine.alltimes))
        $('#td_alllength').html(haoutil.str.formatLength(roamLine.alllen))

        roamLine.on(mars3d.EventType.change, (event) => {
          //面板显示相关信息
          showRealTimeInfo(event)
        })
      }

      function initUI() {
        //模型方向
        // $(".jiaodu_2_angele").hide();
        $('input:radio[name="jiaodu"]').change(function () {
          var isAuto = $(this).val() === '1'
          if (isAuto) {
            $('.jiaodu_2_angele').hide()
          } else {
            $('.jiaodu_2_angele').show()
          }

          updateModel(isAuto)
        })
        //滑动条
        $('#model_pitch').range({
          onChange: function (value, bfb) {
            updateModel()
          },
        })
        $('#model_roll').range({
          onChange: function (value, bfb) {
            updateModel()
          },
        })

        updateModel(false)
      }

      function updateModel(isAuto) {
        var pitch = Number($('#model_pitch').val())
        var roll = Number($('#model_roll').val())

        roamLine.updateAngle(isAuto, {
          pitch: pitch,
          roll: roll,
        })
      }

      //显示实时坐标和时间
      function showRealTimeInfo(params) {
        if (params == null) {
          return
        }

        var val = Math.ceil((params.time * 100) / roamLine.alltimes)
        if (val < 1) {
          val = 1
        }
        if (val > 100) {
          val = 100
        }

        $('.progress-bar')
          .css('width', val + '%')
          .attr('aria-valuenow', val)
          .html(val + '%')

        $('#td_jd').html(params.lng)
        $('#td_wd').html(params.lat)
        $('#td_gd').html(haoutil.str.formatLength(params.alt))

        $('#td_times').html(haoutil.str.formatTime(params.time))
        $('#td_length').html(haoutil.str.formatLength(params.len))

        if (params.hbgd) {
          $('#td_dmhb').html(haoutil.str.formatLength(params.hbgd))
        } else {
          $('#td_dmhb').html('未知')
        }

        if (params.ldgd) {
          $('#td_ldgd').html(haoutil.str.formatLength(params.ldgd))
        } else {
          $('#td_ldgd').html('未知')
        }
      }

      function testShading() {
        //创建矢量数据图层
        var graphicLayer = new mars3d.layer.GraphicLayer()
        map.addLayer(graphicLayer)

        //卫星朝向的中线地面点
        let line1 = new mars3d.graphic.PolylineEntity({
          positions: new Cesium.CallbackProperty(function (time) {
            var pt1 = roamLine.position,
              pt2 = centerPosion
            if (!pt1 || !pt2) {
              return []
            }

            return [pt1, pt2]
          }, false),
          style: {
            width: 2,
            color: '#ff0000',
            arcType: Cesium.ArcType.NONE,
          },
        })
        graphicLayer.addGraphic(line1)

        //卫星边线2点
        let graphicTriangle = new mars3d.graphic.PolylineEntity({
          positions: new Cesium.CallbackProperty(function (time) {
            var positions = getFourShadingPosition({
              angle: 2.4,
              angle2: 1.4,
            })
            return positions
          }, false),
          style: {
            width: 2,
            color: '#0000ff',
            clampToGround: true,
          },
        })
        graphicLayer.addGraphic(graphicTriangle)

        setInterval(function () {
          if (!map.clock.shouldAnimate || !thisPositions) {
            return
          }

          addPolygon()
        }, 1500)

        $('#btnClear').click(function () {
          removeAllPolygon()
        })
      }

      //获取地面的四棱台投影面
      var thisPositions
      var centerPosion

      function getFourShadingPosition(opts) {
        //位置
        var pt1 = roamLine.position
        if (!pt1) {
          return
        }

        var ellipsoid = map.scene.globe.ellipsoid

        //张角
        var angle1 = Cesium.Math.toRadians(opts.angle) / 2

        var heading = Cesium.defaultValue(opts.heading, roamLine.heading)
        var pitch = Cesium.defaultValue(opts.pitch, roamLine.pitch)
        var roll = Cesium.defaultValue(opts.heading, roamLine.roll)

        // var ptLeft1 = mars3d.PointUtil.getRayEarthPosition(pt1, new Cesium.HeadingPitchRoll(heading, pitch, roll + angle1), true, ellipsoid);
        // var ptRight1 = mars3d.PointUtil.getRayEarthPosition(pt1, new Cesium.HeadingPitchRoll(heading, pitch, roll - angle1), true, ellipsoid);
        // if (!ptLeft1 || !ptRight1) return
        // thisPositions = [ptLeft1, ptRight1]

        //张角
        var angle2 = Cesium.Math.toRadians(opts.angle2) / 2

        var ptLeft1 = mars3d.PointUtil.getRayEarthPosition(pt1, new Cesium.HeadingPitchRoll(heading, pitch + angle2, roll + angle1), true, ellipsoid)
        if (!ptLeft1) {
          return
        }

        var ptRight1 = mars3d.PointUtil.getRayEarthPosition(pt1, new Cesium.HeadingPitchRoll(heading, pitch + angle2, roll - angle1), true, ellipsoid)
        if (!ptRight1) {
          return
        }

        var ptRight2 = mars3d.PointUtil.getRayEarthPosition(pt1, new Cesium.HeadingPitchRoll(heading, pitch - angle2, roll - angle1), true, ellipsoid)
        if (!ptRight2) {
          return
        }

        var ptLeft2 = mars3d.PointUtil.getRayEarthPosition(pt1, new Cesium.HeadingPitchRoll(heading, pitch - angle2, roll + angle1), true, ellipsoid)
        if (!ptLeft2) {
          return
        }

        var ptLeft = Cesium.Cartesian3.midpoint(ptLeft1, ptLeft2, new Cesium.Cartesian3())
        var ptRight = Cesium.Cartesian3.midpoint(ptRight1, ptRight2, new Cesium.Cartesian3())
        thisPositions = [ptLeft, ptRight]

        centerPosion = Cesium.Cartesian3.midpoint(ptLeft, ptRight, new Cesium.Cartesian3())

        return [ptLeft1, ptRight1, ptRight2, ptLeft2, ptLeft1]
      }

      var arrImagingAreaPrimitive = []
      var lastPositions

      function addPolygon() {
        if (lastPositions == null) {
          lastPositions = thisPositions
          return
        }

        var positions = [lastPositions[0], lastPositions[1], thisPositions[1], thisPositions[0]]
        lastPositions = thisPositions

        var redPolygonInstance = new Cesium.GeometryInstance({
          geometry: Cesium.PolygonGeometry.fromPositions({
            positions: positions,
            vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
          }),
          attributes: {
            color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED.withAlpha(0.3)),
          },
        })

        if (arrImagingAreaPrimitive.length > 2000) {
          $(arrImagingAreaPrimitive).each(function (i, item) {
            map.scene.primitives.remove(item)
          })
          arrImagingAreaPrimitive = []
        }

        var primitiveImagingArea = map.scene.primitives.add(
          new Cesium.GroundPrimitive({
            //GroundPrimitive        Primitive
            geometryInstances: [redPolygonInstance],
            appearance: new Cesium.PerInstanceColorAppearance({
              closed: true,
              translucent: true,
            }),
          })
        )
        arrImagingAreaPrimitive.push(primitiveImagingArea)

        return primitiveImagingArea
      }

      function removeAllPolygon() {
        $(arrImagingAreaPrimitive).each(function (i, item) {
          map.scene.primitives.remove(item)
        })
        arrImagingAreaPrimitive = []
      }
    </script>
  </body>
</html>
